package config

import (
	"gopkg.in/yaml.v3"
	"io/ioutil"
	"log"
	"path/filepath"
)

type Config struct {
	TcpServer   string `yaml:"tcp_server"`
	MysqlServer MySQL  `yaml:"database"`
	RedisServer Redis  `yaml:"redis"`
}

type MySQL struct {
	Driver             string `yaml:"driver"`
	DbName             string `yaml:"db_name"`
	Username           string `yaml:"username"`
	Password           string `yaml:"password"`
	Hostname           string `yaml:"hostname"`
	MaxConnections     int    `yaml:"max_connections"`
	MaxIdleConnections int    `yaml:"max_idle_connections"`
}

type Redis struct {
	Address  string `yaml:"address"`
	Password string `yaml:"password"`
	DB       int    `yaml:"db"`
}

var config Config

func OpenConfigFile() {
	configFile, _ := filepath.Abs("config/config.yml")
	yamlFile, err := ioutil.ReadFile(configFile)
	err = yaml.Unmarshal(yamlFile, &config)
	if err != nil {
		log.Printf("Error %s when opening config file: ", err)
	}
}

func GetTCPServer() string {
	return config.TcpServer
}

func GetMYSQLServer() MySQL {
	return config.MysqlServer
}

func GetRedisServer() Redis {
	return config.RedisServer
}
